<?php

namespace app\admin\model\kefu;

use ba\Date;
use think\Model;
use think\facade\Config;
use app\worker\library\kefu\Common;

/**
 * User
 */
class User extends Model
{
    // 表名
    protected $name = 'kefu_user';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    protected $updateTime         = false;

    // 追加属性
    protected $append = [
        'group',
    ];

    // 字段类型转换
    protected $type = [
        'last_login_time'         => 'timestamp:Y-m-d H:i:s',
        'join_time'               => 'timestamp:Y-m-d H:i:s',
        'csr_last_reception_time' => 'timestamp:Y-m-d H:i:s',
    ];


    public function getGroupAttr($value, $row): array
    {
        return [
            'name' => \app\admin\model\kefu\Group::whereIn('id', $row['group_ids'])->column('name'),
        ];
    }

    public function getGroupIdsAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setGroupIdsAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function sysUser(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\User::class, 'user_id', 'id');
    }

    public function admin(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\Admin::class, 'admin_id', 'id');
    }

    /**
     * 获取可输出字段
     */
    public static function outputFields(array $fields, $data): array
    {
        $returnData = [];

        if (!$data) {
            return [];
        }

        // 头像
        if (in_array('avatar', $fields)) {
            $returnData['avatar'] = Common::trueValue([
                $data->admin?->avatar,
                $data->sysUser?->avatar,
                Config::get('buildadmin.default_avatar'),
            ]);
        }

        // 昵称
        if (in_array('nickname', $fields)) {
            $returnData['nickname'] = Common::trueValue([
                $data->nickname,
                $data->admin?->nickname,
                $data->sysUser?->nickname,
                Common::getTouristNickname($data->id),
            ]);
        }

        // 备注
        if (in_array('remark', $fields)) {
            $returnData['remark'] = Common::trueValue([
                $data->remark,
                $data->admin?->motto,
                $data->sysUser?->motto,
            ]);
        }

        // 时间
        if (in_array('create_time', $fields)) {
            $returnData['create_time'] = Date::human($data->getData('create_time'));
        }
        if (in_array('last_login_time', $fields)) {
            $returnData['last_login_time'] = Date::human($data->getData('last_login_time'));
        }

        foreach ($fields as $field) {
            if (isset($data[$field]) && !isset($returnData[$field])) {
                $returnData[$field] = $data->$field;
            }
        }
        return $returnData;
    }
}